home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / comm1 / intsdkss.lha / include / sys / malloc.h < prev    next >
C/C++ Source or Header  |  1996-04-09  |  8KB  |  238 lines

  1. /*
  2.  * @(#)malloc.h: $Revision: 1.7.61.9 $ $Date: 92/04/19 21:33:13 $
  3.  * $Locker:  $
  4.  */
  5.  
  6. /*
  7.  * Copyright (c) 1987 Regents of the University of California.
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms are permitted
  11.  * provided that the above copyright notice and this paragraph are
  12.  * duplicated in all such forms and that any documentation,
  13.  * advertising materials, and other materials related to such
  14.  * distribution and use acknowledge that the software was developed
  15.  * by the University of California, Berkeley.  The name of the
  16.  * University may not be used to endorse or promote products derived
  17.  * from this software without specific prior written permission.
  18.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  19.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  20.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21.  *
  22.  *    @(#)malloc.h    7.7 (Berkeley) 6/27/88
  23.  */
  24.  
  25. #ifndef _SYS_MALLOC_INCLUDED /* allows multiple inclusion */
  26. #define _SYS_MALLOC_INCLUDED
  27.  
  28. #define MAXALLOCSAVE (NBPG)     /* Stop using powers-of-two at a page */
  29. #define MAXBUCKETSAVE (PGSHIFT) /* Stop using powers-of-two at a page */
  30. #define MINBUCKET  (5)          /* Minimum allocation size--32 bytes */
  31. #define MAXBUCKET (MINBUCKET+15)/* Max allocation size */
  32.  
  33. /*
  34.  * flags to malloc
  35.  */
  36. #define M_WAITOK    0x0000
  37. #define M_NOWAIT    0x0001
  38. #define M_ALIGN     0x0002
  39. #define M_EXECUTE    0x0004
  40.  
  41. /*
  42.  * Types of memory to be allocated
  43.  */
  44. #define M_FREE        0    /* should be on free list */
  45. #define M_MBUF        1    /* network memory buffer */
  46. #define M_DYNAMIC    2    /* dynamic (data) allocation */
  47. #define M_HEADER    3    /* packet header */
  48. #define M_SOCKET    4    /* socket structure */
  49. #define M_PCB        5    /* protocol control block */
  50. #define M_RTABLE    6    /* routing tables */
  51. #define M_HTABLE    7    /* IMP host tables */
  52. #define M_ATABLE    8    /* address resolution tables */
  53. #define M_SONAME    9    /* socket name */
  54. #define M_SOOPTS    10    /* socket options */
  55. #define M_FTABLE    11    /* fragment reassembly header */
  56. #define M_RIGHTS    12    /* access rights */
  57. #define M_IFADDR    13    /* interface address */
  58. #define M_NTIMO     14    /* network timeouts */
  59. #define M_DEVBUF    15    /* device driver memory */
  60. #define M_ZOMBIE    16    /* zombie proc status */
  61. #define M_NAMEI     17    /* namei path name buffer */
  62. #define M_GPROF     18    /* kernel profiling buffer */
  63. #define M_IOCTLOPS    19    /* ioctl data buffer */
  64. #define M_SUPERBLK    20    /* super block data */
  65. #define M_CRED        21    /* credentials */
  66. #define M_TEMP        22    /* misc temporary data buffers */
  67. #define M_VAS        23    /* VAS structure */
  68. #define M_PREG        24    /* Pregion structure */
  69. #define M_REG        25    /* Region structure */
  70. #define M_IOSYS     26    /* I/O system structure */
  71. #define M_NIPCCB    27    /* Netipc control block */
  72. #define M_NIPCSR    28    /* Netipc socket registry name record */
  73. #define M_NIPCPR    29    /* Netipc path report */
  74. #define M_DQUOTA    30    /* Disk Quotas Cache entries */
  75. #define M_DMA        31    /* Snakes I/O dma services */
  76. #define M_GRAF        32    /* Non-ITE graphics */
  77. #define M_ITE        33    /* ITE (Internal Terminal Emulator) */
  78. #define M_ATL        34    /* S300 attach list entries */
  79. #define M_LOCKLIST    35    /* Locklist entry */
  80. #define M_DBC        36    /* Related to dynamic buffer cache */
  81. #define M_LOSTPAGE    37    /* VM copy avoidance lost page list entries */
  82. #define M_COWARGS    38    /* VM copy-on-write reference descriptions */
  83. #define M_LAST        39
  84.  
  85.     /*
  86.      * If adding new types and changing M_LAST, remember to modify
  87.      * sys/vm_misc.c and analyze/standard/analyze.c, as well.
  88.      */
  89.  
  90.  
  91. #define btokup(va) ((struct kmemusage *)PAGE_INFO((caddr_t)va))
  92.  
  93. struct kmemstats {
  94.     int    ks_flags;    /* Flags */
  95.     int    (*ks_check)();
  96.     int    ks_sleep;    /* Some one is waiting for this bucket */
  97.     long    ks_reslimit;    /* Total reserved bytes allowed */
  98.     long    ks_resinuse;    /* Total reserved bytes in use */
  99. };
  100.  
  101. #define KS_RESERVED 0x01
  102.  
  103. /*
  104.  * Array of descriptors that describe the contents of each page
  105.  */
  106. struct kmemusage {
  107.     u_short ku_indx;        /* bucket index */
  108.     u_short ku_pagecnt;    /* for large allocations, */
  109.                 /* pages allocated, else 0 */
  110. };
  111.  
  112. /*
  113.  * Set high order bit in a short.
  114.  */
  115. #define RESERVED_POOL (1 << 15)
  116. #define MAXPGCNT (~RESERVED_POOL)
  117.  
  118. /*
  119.  * Set of buckets for each size of memory block that is retained
  120.  */
  121. struct kmembuckets {
  122.     caddr_t kb_next;    /* list of free blocks */
  123.     long    kb_calls;    /* total calls to allocate this size */
  124.     long    kb_total;    /* total number of pages allocated to a */
  125.                 /* bucket (even if some elements are free) */
  126.     long    kb_totalfree;    /* # of free elements in this bucket */
  127. #ifdef LATER
  128.     long    kb_elmpercl;    /* # of elements in this sized allocation */
  129.     long    kb_highwat;    /* high water mark */
  130.     long    kb_couldfree;    /* over high water mark and could free */
  131. #endif /* LATER */
  132. };
  133.  
  134. #ifdef _KERNEL
  135.  
  136. caddr_t kmalloc();
  137. caddr_t kmem_alloc();
  138.  
  139. #define MINALLOCSIZE    (1 << MINBUCKET)
  140. #define BUCKETINDX(size) \
  141.     ((size) <= (MINALLOCSIZE * 128) \
  142.         ? (size) <= (MINALLOCSIZE * 8) \
  143.             ? (size) <= (MINALLOCSIZE * 2) \
  144.                 ? (size) <= (MINALLOCSIZE * 1) \
  145.                     ? (MINBUCKET + 0) \
  146.                     : (MINBUCKET + 1) \
  147.                 : (size) <= (MINALLOCSIZE * 4) \
  148.                     ? (MINBUCKET + 2) \
  149.                     : (MINBUCKET + 3) \
  150.             : (size) <= (MINALLOCSIZE* 32) \
  151.                 ? (size) <= (MINALLOCSIZE * 16) \
  152.                     ? (MINBUCKET + 4) \
  153.                     : (MINBUCKET + 5) \
  154.                 : (size) <= (MINALLOCSIZE * 64) \
  155.                     ? (MINBUCKET + 6) \
  156.                     : (MINBUCKET + 7) \
  157.         : (size) <= (MINALLOCSIZE * 2048) \
  158.             ? (size) <= (MINALLOCSIZE * 512) \
  159.                 ? (size) <= (MINALLOCSIZE * 256) \
  160.                     ? (MINBUCKET + 8) \
  161.                     : (MINBUCKET + 9) \
  162.                 : (size) <= (MINALLOCSIZE * 1024) \
  163.                     ? (MINBUCKET + 10) \
  164.                     : (MINBUCKET + 11) \
  165.             : (size) <= (MINALLOCSIZE * 8192) \
  166.                 ? (size) <= (MINALLOCSIZE * 4096) \
  167.                     ? (MINBUCKET + 12) \
  168.                     : (MINBUCKET + 13) \
  169.                 : (size) <= (MINALLOCSIZE * 16384) \
  170.                     ? (MINBUCKET + 14) \
  171.                     : (MINBUCKET + 15))
  172.  
  173. /*
  174.  * Macro versions for the usual cases of kmalloc/kfree
  175.  * Be sure that modifications are made to both the
  176.  * OSDEBUG and non-OSDEBUG versions.
  177.  */
  178. #define MALLOC(space, cast, size, type, flags) { \
  179.     struct kmembuckets *kbp = &bucket[BUCKETINDX(size)]; \
  180.     register int context; \
  181.     SPINLOCK_USAV(kmemlock, context); \
  182.     if ((size > MAXALLOCSAVE) || (kbp->kb_next == NULL)) { \
  183.         SPINUNLOCK_USAV(kmemlock, context); \
  184.         (space) = (cast)kmalloc((u_long)(size), type, flags); \
  185.     } else { \
  186.         (space) = (cast)kbp->kb_next; \
  187.         kbp->kb_next = *(caddr_t *)(space); \
  188.         SPINUNLOCK_USAV(kmemlock, context); \
  189.     } \
  190. }
  191.  
  192. /* Use this macro when "size" is compile time known else use vapor_malloc() */
  193. #define VAPOR_MALLOC(space, cast, size, type, flags) \
  194. { \
  195.     struct    Vm { \
  196.         struct    Vm    *v_link; }; \
  197.     MALLOC((space), cast, ((size)+sizeof(struct Vm)), (type), (flags)); \
  198.     if (space) { \
  199.         ((struct Vm *)(space))->v_link = (struct Vm *)u.u_vapor_mlist; \
  200.         u.u_vapor_mlist = (caddr_t)(space); \
  201.         (struct Vm *)(space) += 1; \
  202.     } \
  203. }
  204.  
  205. extern void kfree();
  206.  
  207. #define FREE(x, type) { \
  208.     struct kmemusage *kup = btokup((caddr_t)x); \
  209.     if (kup->ku_indx > MAXBUCKETSAVE) { \
  210.         kfree((caddr_t)(x), type); \
  211.     } else { \
  212.         register int context;        \
  213.         struct kmembuckets *kbp = &bucket[kup->ku_indx]; \
  214.         SPINLOCK_USAV(kmemlock, context); \
  215.         *(caddr_t *)(x) = kbp->kb_next; \
  216.         kbp->kb_next = (caddr_t)(x); \
  217.         SPINUNLOCK_USAV(kmemlock, context); \
  218.     } \
  219. }
  220.  
  221. extern struct kmembuckets bucket[];
  222. extern struct kmembuckets reserved_bucket[];
  223. extern struct kmemstats kmemstats[];
  224.  
  225. extern vm_lock_t kmemlock;
  226.  
  227. /*
  228.  * Defines for calling kalloc (low level memory allocator).
  229.  */
  230. #define KALLOC_NOWAIT    0x01
  231. #define KALLOC_NOZERO    0x02
  232. #define KALLOC_NORESV    0x04
  233.  
  234. extern caddr_t kalloc();
  235.  
  236. #endif /* _KERNEL */
  237. #endif /* _SYS_MALLOC_INCLUDED */
  238.